// pages/comment/comment.js

import { createStoreBindings } from 'mobx-miniprogram-bindings';
import { store } from '../../store/store';
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    comment: '',
    value: 0,
    urlPost: "/portal/comment/post",
  },

  onChangeComment(event) {
    // event.detail 为当前输入的值
    this.setData({
      comment: event.detail,
    });
  },

  onChangeValue(event) {
    this.setData({
      value: event.detail,
    });
  },

  submit(e) {
    if (this.data.comment == '' || this.data.value == 0) {
      Toast("请评价和打分");
      return;
    }

    Dialog.confirm({
      message: '确认提交评论',
    })
      .then(() => {
        // on confirm
        Toast.loading({
          message: '加载中...',
          forbidClick: true,
          loadingType: 'spinner',
        });

        wx.request({
          url: this.data.urlPost,
          method: "POST",
          data: {
            userId: this.getUser().id,
            orderId: this.data.id,
            score: this.data.value,
            comm: this.data.comment,
          },
          success: (res) => {
            Toast(res.data.msg);
            wx.navigateBack();
          },
          complete: () => {
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    const eventChannel = this.getOpenerEventChannel();
    // 监听 acceptDataFromOpenerPage 事件，获取上一页面通过 eventChannel 传送到当前页面的数据
    let acceptId = 0;
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      // 获取商品id
      acceptId = data.data;
    });
    this.setData({
      id: acceptId,
    });


    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['user'],
      actions: ['getUser', 'getWebsite']
    });

    this.setData({
      urlPost: this.getWebsite() + this.data.urlPost,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 销毁挂载绑定
    this.storeBindings.destroyStoreBindings()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})